package com.magiccompass.alipay.barcode.responsehandler;

import org.w3c.dom.Document;

import com.magiccompass.alipay.barcode.entity.AlipayQRCodeCreateRequest;
import com.magiccompass.alipay.barcode.entity.AlipayQRCodeCreateResponse;
import com.magiccompass.alipay.barcode.model.AlipayQRCodeCreateResponseModel;
import com.magiccompass.alipay.barcode.service.AlipayBarcodeService;
import com.magiccompass.alipay.barcode.util.AlipayBarcodeResponseUtil;
import com.magiccompass.alipay.barcode.util.XmlUtil;

public class QRCodeCreateResponseHandler {
	public static AlipayQRCodeCreateResponseModel handleResponse(String response,AlipayQRCodeCreateRequest aqrccr, AlipayBarcodeService alipayBarcodeService){
		AlipayQRCodeCreateResponseModel abprm = getResponseResult(response,aqrccr, alipayBarcodeService);
		return abprm;
	}	
	
	private static AlipayQRCodeCreateResponseModel getResponseResult(String response, AlipayQRCodeCreateRequest aqrccr, AlipayBarcodeService alipayBarcodeService){
		Document xmlDoc = XmlUtil.makeXmlDocumentFromString(response);		
		AlipayQRCodeCreateResponseModel aqrccrm = new AlipayQRCodeCreateResponseModel();
		
		String responseIsSuccess = xmlDoc.getElementsByTagName("is_success").item(0).getTextContent();
		String sign = xmlDoc.getElementsByTagName("sign").item(0)==null?null:xmlDoc.getElementsByTagName("sign").item(0).getTextContent();
		String signType = xmlDoc.getElementsByTagName("sign_type").item(0)==null?null:xmlDoc.getElementsByTagName("sign_type").item(0).getTextContent();
		
		aqrccrm.setIsSuccess(responseIsSuccess);
		aqrccrm.setSign(sign);
		aqrccrm.setSignType(signType);
		
		if("T".equals(aqrccrm.getIsSuccess())){
			String qrCode = xmlDoc.getElementsByTagName("qrcode").item(0).getTextContent();
			String qrCodeImgUrl = xmlDoc.getElementsByTagName("qrcode_img_url").item(0).getTextContent();
			aqrccrm.setQrCode(qrCode);
			aqrccrm.setQrCodeImgUrl(qrCodeImgUrl);
		}else if("F".equals(aqrccrm.getIsSuccess())){
			aqrccrm.setResultCode("-1");
			String error = xmlDoc.getElementsByTagName("error").item(0)==null?null:xmlDoc.getElementsByTagName("error").item(0).getTextContent();
			aqrccrm.setError(error);
		}else{
			aqrccrm.setResultCode("-2"); // ALIPAY response neither "T" nor "F"
			aqrccrm.setError("UNEXPECTED EXCEPTION!");
		}
		AlipayQRCodeCreateResponse aqrccres = AlipayBarcodeResponseUtil.createAlipayQRCodeCreateReponseEntityFromModelAndRequest(aqrccrm, aqrccr);
		alipayBarcodeService.save(aqrccres);
		return aqrccrm;
	}
	
	public static void main(String[] args){
		//AlipayBarcodePaymentResponseModel abprm = getResponseResult(RESULT_PAYMENT_SUCCESS_4_TEST);
		//System.out.println(abprm);

	}
	
	public static final String RESULT_PAYMENT_SUCCESS_4_TEST = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><alipay><is_success>T</is_success><request><param name=\"trans_create_time\">20160323103315</param><param name=\"identity_code_type\">barcode</param><param name=\"alipay_seller_id\">2088021966645500</param><param name=\"sign_type\">RSA</param><param name=\"trans_amount\">61.87</param><param name=\"trans_name\">PAYMENT_BY_ALIPAY_BARCODE</param><param name=\"partner_trans_id\">104</param><param name=\"buyer_identity_code\">123456789012345678</param><param name=\"currency\">AUD</param><param name=\"sign\">T0cPCjvrtoOMgMx5kztMXJj1k7BquXSszD0yXK9IoGYt/PwDYK6UUZiMWN41QUpjGZD1uDMKJWhtRMfw7Zwsd5/ieQ3ODB/OMy6FN6zCrztUr4v0rpPvyCRTI9DWR8iVqPay64lT3335AbvqD3wp2zqw97xbQ9e2ykmH/7VVrlQ=</param><param name=\"_input_charset\">UTF-8</param><param name=\"biz_product\">OVERSEAS_MBARCODE_PAY</param><param name=\"service\">alipay.acquire.overseas.spot.pay</param><param name=\"partner\">2088021966645500</param></request><response><alipay><alipay_trans_id>2011091703338463</alipay_trans_id><partner_trans_id>201311221000000002</partner_trans_id><alipay_buyer_user_id>2088102130896433</alipay_buyer_user_id><alipay_buyer_login_id>cao***@126.com</alipay_buyer_login_id><alipay_pay_time>20131120155823</alipay_pay_time><exchange_rate>6.0939</exchange_rate><currency>AUD</currency><trans_amount>39.25</trans_amount><trans_amount_CNY>239.19</trans_amount_CNY><result_code>SUCCESS</result_code></alipay></response><sign>D/UTFJt5yh3rxNXSE5botyzpyv9vDe/QbJpLgJ98j3j9HtGcVHpUtPyFr/3ZuVt3VFJ4XI9v41/wqe1qBEeTjffg4hmXOWF1nc8+ePfZNi883Qgl7a4d3hzcXzr9j1lsK33cJeruLEBeQOiu7umd3jPBHdBSBx9qt7DbeLgbtKM=</sign><sign_type>RSA</sign_type></alipay>";
	public static final String RESULT_PAYMENT_FAILED_4_TEST = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><alipay><is_success>T</is_success><request><param name=\"trans_create_time\">20160323103315</param><param name=\"identity_code_type\">barcode</param><param name=\"alipay_seller_id\">2088021966645500</param><param name=\"sign_type\">RSA</param><param name=\"trans_amount\">61.87</param><param name=\"trans_name\">PAYMENT_BY_ALIPAY_BARCODE</param><param name=\"partner_trans_id\">104</param><param name=\"buyer_identity_code\">123456789012345678</param><param name=\"currency\">AUD</param><param name=\"sign\">T0cPCjvrtoOMgMx5kztMXJj1k7BquXSszD0yXK9IoGYt/PwDYK6UUZiMWN41QUpjGZD1uDMKJWhtRMfw7Zwsd5/ieQ3ODB/OMy6FN6zCrztUr4v0rpPvyCRTI9DWR8iVqPay64lT3335AbvqD3wp2zqw97xbQ9e2ykmH/7VVrlQ=</param><param name=\"_input_charset\">UTF-8</param><param name=\"biz_product\">OVERSEAS_MBARCODE_PAY</param><param name=\"service\">alipay.acquire.overseas.spot.pay</param><param name=\"partner\">2088021966645500</param></request><response><alipay><error>INVALID_PARAMETER</error><result_code>FAILED</result_code></alipay></response><sign>D/UTFJt5yh3rxNXSE5botyzpyv9vDe/QbJpLgJ98j3j9HtGcVHpUtPyFr/3ZuVt3VFJ4XI9v41/wqe1qBEeTjffg4hmXOWF1nc8+ePfZNi883Qgl7a4d3hzcXzr9j1lsK33cJeruLEBeQOiu7umd3jPBHdBSBx9qt7DbeLgbtKM=</sign><sign_type>RSA</sign_type></alipay>";
	public static final String RESULT_PAYMENT_UNKNOWN_4_TEST = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><alipay><is_success>T</is_success><request><param name=\"trans_create_time\">20160323103315</param><param name=\"identity_code_type\">barcode</param><param name=\"alipay_seller_id\">2088021966645500</param><param name=\"sign_type\">RSA</param><param name=\"trans_amount\">61.87</param><param name=\"trans_name\">PAYMENT_BY_ALIPAY_BARCODE</param><param name=\"partner_trans_id\">104</param><param name=\"buyer_identity_code\">123456789012345678</param><param name=\"currency\">AUD</param><param name=\"sign\">T0cPCjvrtoOMgMx5kztMXJj1k7BquXSszD0yXK9IoGYt/PwDYK6UUZiMWN41QUpjGZD1uDMKJWhtRMfw7Zwsd5/ieQ3ODB/OMy6FN6zCrztUr4v0rpPvyCRTI9DWR8iVqPay64lT3335AbvqD3wp2zqw97xbQ9e2ykmH/7VVrlQ=</param><param name=\"_input_charset\">UTF-8</param><param name=\"biz_product\">OVERSEAS_MBARCODE_PAY</param><param name=\"service\">alipay.acquire.overseas.spot.pay</param><param name=\"partner\">2088021966645500</param></request><response><alipay><result_code>UNKNOW</result_code></alipay></response><sign>D/UTFJt5yh3rxNXSE5botyzpyv9vDe/QbJpLgJ98j3j9HtGcVHpUtPyFr/3ZuVt3VFJ4XI9v41/wqe1qBEeTjffg4hmXOWF1nc8+ePfZNi883Qgl7a4d3hzcXzr9j1lsK33cJeruLEBeQOiu7umd3jPBHdBSBx9qt7DbeLgbtKM=</sign><sign_type>RSA</sign_type></alipay>";
	public static final String RESULT_PAYMENT_FAILEDFAIED_4_TEST = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><alipay><is_success>F</is_success><error>ILLEGAL_SIGN</error><sign>D/UTFJt5yh3rxNXSE5botyzpyv9vDe/QbJpLgJ98j3j9HtGcVHpUtPyFr/3ZuVt3VFJ4XI9v41/wqe1qBEeTjffg4hmXOWF1nc8+ePfZNi883Qgl7a4d3hzcXzr9j1lsK33cJeruLEBeQOiu7umd3jPBHdBSBx9qt7DbeLgbtKM=</sign><sign_type>RSA</sign_type></alipay>";
}
